<div id="banner">
    <div class="red">
        [$result.msg]
    </div>
	<div class="show_title">
		<h2>添加/编辑广告</h2>
	</div>
    <div>
        <div id="sec_module">
            <!--purpose widget add -->
            <!--<include: file="../part/purpose.html">-->
        </div>
        <div id="main_module">
            <div id="main_module_content">
                <form action="/admin/admin_banner/savebanner" name="savebanner" id="savebanner" method="post">
                    <input type="hidden" name="banner[b_id]" value="[$banner.b_id]"/>
					<input type="hidden" name="banner[b_files]" id="b_files" value=""/>
                    <table>
                        <tr>
                            <td width="200px">
                                广告名称
                            </td>
                            <td>
                                <input type="text" value="[@htmlspecialchars($banner.b_name)]" name="banner[b_name]"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                所属项目
                            </td>
                            <td>
                                <select name="banner[b_project]">
                                	<foreach name="projects" key="key" var="item">
                                		<if $item.p_id==$banner.b_project>
                                			<option selected="true" value="[$item.p_id]">[$item.p_name]</option>
                                		<else>
                                			<option value="[$item.p_id]">[$item.p_name]</option>
                                		</if>
                                	</foreach>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                广告位
                            </td>
                            <td>
                                <select name="banner[b_zone]">
                                	<foreach name="zones" key="key" var="item">
                                		<if $item.z_id==$banner.b_zone>
                                			<option selected="true" value="[$item.z_id]">[$item.z_name]</option>
                                		<else>
                                			<option value="[$item.z_id]">[$item.z_name]</option>
                                		</if>
                                	</foreach>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                模板类型
                            </td>
                            <td>
                                <select id="file_mtype" name="banner[b_mtype]">
                                	<foreach name="modals" key="key" var="item">
                                		<if $item.mt_id==$banner.b_mtype.mt_id>
                                			<option selected="true" is_multi='[$item.mt_is_multi]' value="[$item.mt_id]">[$item.mt_name]</option>
                                		<else>
                                			<option is_multi='[$item.mt_is_multi]' value="[$item.mt_id]">[$item.mt_name]</option>
                                		</if>
                                	</foreach>
                                </select>
								<input type="button" name="additem" 
								<if !$banner||$banner.b_mtype.mt_is_multi=="0">
								 style="display:none" 
								 </if>
								 id="additem" value="添加项">
							</td>
                        </tr>
						<tr>
							<td>
								&nbsp;
							</td>
							<td>
									<if $banner.b_files>
										<foreach name="banner.b_files" key="key" var="item">
											<div class="mt_item">
												<if $key=="0">
													外链：http://<input class="url_link" size="20" value="[$item.f_link]">
													图片连接：<input class="img_link" size="10" value="[$item.f_url]">
													<span url="http://[$item.f_url]" class="info_loading">loading</span>
												<else>
													外链：http://<input class="url_link" size="20"  value="[$item.f_link]">
													图片连接：<input class="img_link" size="10" value="[$item.f_url]">
													<span class="delete_btn"><a>X</a></span>
													<span url="http://[$item.f_url]" class="info_loading">loading</span>
												</if>
											</div>
										</foreach>
									<else>
										<div class="mt_item">
											外链：http://<input class="url_link" size="20"  value="">
											图片连接：<input class="img_link" size="10" value="">
										</div>
									</if>
								</div>
							</td>
						</tr>
                        <tr>
                            <td>
                                排序
                            </td>
                            <td>
                                <input size="10" value="[@htmlspecialchars($banner.sort)]" name="banner[sort]"/>
                            </td>
                        </tr>
                    </table><input type="submit" class="input" name="submit_btn" value="保存"/>
                </form>
            </div>
        </div>
    </div>
    <script>
		$("#additem").die('click').live("click",function(){
			var item = $($(".mt_item")[0]).clone();
			var delitem = $("<span class='delete_btn'><a>X</a></span>")
			item.find(":text").val("").end().find(".info_loading").remove();
			item.find(':text:last').after(delitem);
			$(".mt_item").parent("td").append(item);
		});
		$(".delete_btn").die("click").live("click",function(){
			$(this).parents(".mt_item").fadeOut(300,function(){
				$(this).remove();
			});
		});
		
		$(".img_link").die("click").live('click',function(){
			var $this = $(this);
			var $mt_itme = $this.parents(".mt_item");
			$.facebox({
				ajax:"/admin/admin_file/index"
			});
			$(".select_file").die('click').live("click",function(){
				var url = $(this).attr("url");
				$this.val(url);
				$mt_itme.find(".info_loading").remove();
				$mt_itme.append("<span url=http://"+url+" class='info_loading'>loading</span>");
				jQuery(document).trigger('close.facebox')
			});
			return false;
		});
		$(".info_loading").die("click").live('click',function(){
			var img = $(this).attr("url");
			$.facebox({
				image:img
			});
			return false;
		});
		$("#file_mtype").change(function(){
			var val = $("option:selected",this).attr("is_multi");
			if(val=="1")
			{
				$("#additem").css("display","");
			}
			else
			{
				$("#additem").css("display","none");
			}
			return false;
		});
        $(":submit").live('click', function(e){
			var files = [];
			$(".mt_item").each(function(){
				var f_url  = $(this).find(".img_link").val();
				var f_link = $(this).find(".url_link").val();
				files.push({'f_link':f_link,'f_url':f_url});
			});
			var fjson = JSON.stringify(files);
			$("#b_files").val(fjson);
            $.ajaxSubmit($(this).parents('form'), function(data){
                $.showPageInfo("#page_info", data.info, 500, true);
            });
            return false;
        });
    </script>
